/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.adapter.driven.web.organization;

import com.lap.auth.application.organization.OrgAppService;
import com.lap.auth.application.organization.command.CreateOrgCmd;
import com.lap.auth.application.organization.command.UpdateOrgCmd;
import com.lap.auth.application.organization.dto.GetOrgByIdsQuery;
import com.lap.auth.application.organization.dto.OrgNodeDto;
import com.lap.auth.application.organization.dto.OrgOptionDto;
import com.lap.framework.common.annotation.AuthSkip;
import com.lap.watchdog.starter.annotation.RepeatSubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 提供组织架构restful接口层:
 *
 * <ol>
 *   <li>业务规则，原则上不做任何业务逻辑处理，仅仅是技术入口
 *   <li>命名规则，语义上明确接口做什么，推荐使用 Restful 命名 + Verb风格方法名（适配Swagger、Spring）:
 *       <ul>
 *         <li>分页使用 getXXXPage
 *         <li>获取单个 getXXById
 *         <li>保存使用 createXX
 *         <li>更新使用 updateXX
 *         <li>删除使用 deleteXX
 *       </ul>
 * </ol>
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Tag(name = "组织架构管理", description = "提供组织架构基本操作，查询等")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/v1/organizations")
public class OrgController {

  private final OrgAppService orgAppService;

  @Operation(summary = "获取组织架构树")
  @AuthSkip
  @GetMapping
  public List<OrgNodeDto> getOrgTree() {
    return orgAppService.getOrgTree();
  }

  @Operation(summary = "根据ids获取组织架构")
  @AuthSkip
  @PostMapping("_batch")
  public List<OrgOptionDto> batchGetOrg(@RequestBody @Valid GetOrgByIdsQuery query) {
    return orgAppService.queryOrgOptionByIds(query);
  }

  @Operation(summary = "添加组织架构")
  @RepeatSubmit
  @PostMapping
  public Integer createOrg(@RequestBody @Valid CreateOrgCmd cmd) {
    return orgAppService.createOrg(cmd);
  }

  @Operation(summary = "更新组织架构")
  @RepeatSubmit
  @PutMapping("{id}")
  public Integer updateOrg(@PathVariable Integer id, @RequestBody @Valid UpdateOrgCmd cmd) {
    return orgAppService.updateOrg(id, cmd);
  }

  @Operation(summary = "删除组织架构")
  @RepeatSubmit
  @DeleteMapping("{id}")
  public Integer deleteOrg(@PathVariable Integer id) {
    return orgAppService.deleteOrg(id);
  }
}
